package xc.com.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xc.com.common.Result;
import xc.com.common.StatusCode;
import xc.com.course.domain.CoursePic;
import xc.com.course.feign.OssFegin;
import xc.com.course.mapper.CoursePicMapper;
import xc.com.course.service.CoursePicService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuolizhong
 * @since 2021-06-03
 */
@Service
public class CoursePicServiceImpl extends ServiceImpl<CoursePicMapper, CoursePic> implements CoursePicService {

    @Autowired
    private  CoursePicMapper coursePicMapper;
    @Autowired
    private OssFegin ossFegin;
    @Override
    public Result<String> addCoursePic(CoursePic coursePic) {
        //查询课程图片信息数据
        CoursePic pic = coursePicMapper.selectById(coursePic.getCourseid());
        //判断课程图片信息是否有数据
        if(pic!=null){
            String[] split = pic.getPic().split("/");
            String pid = split[split.length - 1];
            ossFegin.delete(pid);
            //删除课程图片信息
            coursePicMapper.deleteById(pic.getCourseid());
        }
        int i = coursePicMapper.insert(coursePic);
        if(i>0){
            return new   Result<String>(true, StatusCode.OK,"添加成功",coursePic.getPic());
        }
        return new   Result<String>(false, StatusCode.REMOTE_ERROR,"添加失败");
    }

    @Override
    public CoursePic selectById(String courseId) {
        //查询课程图片信息数据
        CoursePic pic = coursePicMapper.selectById(courseId);
        return pic;
    }
}
